﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Persist.BLL.Model;
using CharacterTrailLibrary.Security;
using DevExpress.XtraEditors;
using Telegnosis.Framework.Constants;

namespace CharacterTrailClient.Controls
{
    public partial class ProductRepairHeaderEdit : DevExpress.XtraEditors.XtraUserControl
    {
        private ProductBaseRepairEdit _parentProductBaseEdit = null;
        private readonly IStaticDataQuerier _staticDataQuerier = null;
        private readonly IRepair _repairPersist = null;
        private readonly IBom _bomImpl = null;
        //private readonly List<TRepairHeaderModel> _repairHeaderModels = new List<TRepairHeaderModel>();
        private TRepairHeaderModel _repairHeaderModel = null;
        public ProductRepairHeaderEdit()
        {
            _bomImpl = InstanceFactory.GetBom();
            _repairPersist = InstanceFactory.GetRepairImpl();
            _staticDataQuerier = InstanceFactory.GetStaticDataQuerier();

            InitializeComponent();
            
            this.barCodeAndNameOld.TextBoxInputBarCodeValidatedEnd +=
                (barCodeAndName, printlogModel) => 
                    AddOld(barCodeAndName.BarCode, printlogModel);

            this.barCodeAndNameNew.TextBoxInputBarCodeValidatedEnd +=
            (barCodeAndName, printlogModel) =>
                AddNew(barCodeAndName.BarCode, printlogModel);
        }

        public void SetFocus()
        {
            this.barCodeAndNameOld.Focus();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (GlobalClass.RunTimeMode)
            {
                _parentProductBaseEdit = (ProductBaseRepairEdit) this.Parent;
            }
        }

        public void AddOld(string barCode, lmbcprintlogModel printlogModel)
        {
            if (!_parentProductBaseEdit.AddSuccess)
            {
                Message.MsgBox.ShowError("维修信息没有保存，请先输入保存维修信息！");
                this.barCodeAndNameNew.ResetBarCode();
                this.barCodeAndNameNew.Focus();
                return;
            }

            _repairHeaderModel = null;

            var repairHeaderModelTmp = _repairPersist.GetRepairHeaderModel(barCode);
            if (repairHeaderModelTmp != null)
             {
                Message.MsgBox.ShowError("该产品/物料条码已经维修过！");
                this.barCodeAndNameOld.ResetBarCode();
                 this.barCodeAndNameOld.Focus();
             }
            else
            {
                //查物料表，看是否物料在系统中
                var iCItemModel = _staticDataQuerier.GetVIcItemQueryModel(printlogModel.Fitemid);
                if(iCItemModel == null)
                {
                    Message.MsgBox.ShowError(String.Format("产品/物料不在系统中：{0}", printlogModel.Fitemid));
                    return;
                }

                barCodeAndNameOld.FName = iCItemModel.FName;
                barCodeAndNameOld.FlmbcprintlogModel = printlogModel;
                _repairHeaderModel = new TRepairHeaderModel();

                _repairHeaderModel.ProductBaseRowGuid = _parentProductBaseEdit.ProductBaseModel.RowGuid;

                _repairHeaderModel.EditFlag = Enums.EditFlag.New;
                _repairHeaderModel.InputDate = DateTime.Now;

                _repairHeaderModel.UseStatus = 1;
                _repairHeaderModel.InputId = UserSession.CurrentUser.UserId;
                _repairHeaderModel.ExtChar4 = UserSession.CurrentUser.UserName;

                _repairHeaderModel.ProductItemBarCode = barCode;

                _repairHeaderModel.ProductItemId = printlogModel.Fitemid;

                _repairHeaderModel.ProductItemName = iCItemModel.FName;
                _repairHeaderModel.ProductItemNumber = iCItemModel.FNumber;

                this.barCodeAndNameNew.ResetBarCode();
                this.barCodeAndNameNew.Focus();
            }
        }

        public void AddNew(string barCode, lmbcprintlogModel printlogModel)
        {
            if (_repairHeaderModel != null)
            {
                //查物料表，看是否物料在系统中
                var iCItemModel = _staticDataQuerier.GetVIcItemQueryModel(printlogModel.Fitemid);
                if (iCItemModel == null)
                {
                    Message.MsgBox.ShowError(String.Format("产品/物料不在系统中：{0}", printlogModel.Fitemid));
                    return;
                }

                barCodeAndNameNew.FName = iCItemModel.FName;
                _repairHeaderModel.NewProductItemBarCode = barCode;

                _repairHeaderModel.NewProductItemId = printlogModel.Fitemid;

                _repairHeaderModel.NewProductItemName = iCItemModel.FName;
                _repairHeaderModel.NewProductItemNumber = iCItemModel.FNumber;

                Save();

                this._parentProductBaseEdit.FRepairHeaderViewControl.AppendRepairHeaderModel(_repairHeaderModel);
            }
        }

        private void Save()
        {
            if (_repairHeaderModel != null)
            {
                _repairPersist.SaveRepairHeaderModel(_repairHeaderModel);
                this.barCodeAndNameNew.ResetBarCode();
                this.barCodeAndNameOld.ResetBarCode();
                this.barCodeAndNameOld.Focus();
            }
        }
    }
}
